//
//  RwdNetworkTools.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/12.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import AFNetworking

//只有两种post和get所以用枚举来写
enum RequestType: Int{
    case POST
    case GET
    
}

class RwdNetworkTools: AFHTTPSessionManager {
    //创建一个网络工具的单例
    static let sharedTools: RwdNetworkTools = {
        //创建单例对象
        let tools = RwdNetworkTools()
        //添加接受的返回数据的类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    /**
     网络请求的工具类的各种参数
     
     - parameter type:     网络请求的类别 GET/POST
     - parameter url:      接口
     - parameter params:   参数
     - parameter callBack: 回调收到的数据
     */
    
    func request(type: RequestType, url: String, params: AnyObject?, callBack: (response: AnyObject?, error: NSError?)->()) {
        //如果是get请求
        if type == .GET {
            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        } else {
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        }
    }

    
    

}

extension RwdNetworkTools {
    
    func loadStatus(maxId: Int64 = 0, sinceId: Int64 = 0,callBack: (response: AnyObject?, error: NSError?) -> ()) {
        //准备接口地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //准备参数
        let prams = [
            "access_token": RwduserAccountViewModel.shareUserAccount.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        
        //打印请求信息
        print(url + "?access_token=" + RwduserAccountViewModel.shareUserAccount.accessToken!)
        //发送请求
        
        request(.GET, url: url, params: prams, callBack: callBack)
        
    }
    
}

extension RwdNetworkTools{
    
    //  通过授权码(code)获取accesstoken
    func requestAccesstoken(code: String, callBack: (response: AnyObject?, error: NSError?) -> ()) {
        //  准备接口地址
        let url = "https://api.weibo.com/oauth2/access_token"
        //  准备参数
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Uri
        ]
        
        //  使用通用接口去请求
        request(.POST, url: url, params: params, callBack: callBack)
        
        
        
    }
    

    
    
    
    
    func getuserInfos(userAccount: RwdUserAcount, callBack: (response: AnyObject?, error: NSError?) -> ()){
        
        
        //          请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        
        let prams = [
            "access_token":userAccount.access_token!,
            "uid":"\(userAccount.uid)"
            
        ]
        
        request(.GET, url: url, params: prams, callBack: callBack)
                
        }
        
        
        
    }

    
